#include <REGX52.H>
#include "DS18B20.h"
#include "Delay.h"
#include "LCD1602.h"
#include "DS1302.h"
#include "Key.h"
#include "Timer1.h"
#include "TimeShow.h"
#include "Clock.h"
#include "Buzzer.h"
#include "IR.h"


unsigned char KeyNum, MODE,Command;
unsigned char IR_KeyNumber;

float T, TShow;

//void T_Get()
//{
//	DS18B20_ConvertT();
//	T = DS18B20_ReadT();
//}

void T_Show()
{
		DS18B20_ConvertT();	
		T = DS18B20_ReadT();	
		LCD_ShowString(1,1,"Temperature:");	
		if(T<0)
		{
			LCD_ShowChar(2,1,'-');
			TShow = -T;
		}
		else
		{
			TShow = T;
			LCD_ShowChar(2,1,'+');
		}	
		LCD_ShowNum(2,2,TShow,3);
		LCD_ShowChar(2,5,'.');
		LCD_ShowNum(2,6,(unsigned long)(TShow*10)%10,1);
}

void T_Alarm()
{
	DS18B20_ConvertT();
	T = DS18B20_ReadT();
	LCD_ShowNum(2,12,T,3);
	if(T > 26)
	{
		P2=0;
	}
	else
	{
		P2=0xFF;
	}
}


unsigned char IR_GetState()
{
	unsigned char Command;
	unsigned char IR_keyNumber=0;
		if(IR_GetDataFlag())
		{
			Command=IR_GetCommand();
			if(Command == IR_MODE){IR_keyNumber = 1;}//模式改变
			if(Command == IR_NEXT){IR_keyNumber = 2;}//光标后移
			if(Command == IR_PREVIOUS){IR_keyNumber = 3;}//光标前移
			if(Command == IR_VOL_ADD){IR_keyNumber = 4;}//值加
			if(Command == IR_VOL_MINUS){IR_keyNumber = 5;}//值减
			if(Command == IR_MUTE	){IR_keyNumber = 6;}//关闭闹钟
			if(Command == IR_EQ	){IR_keyNumber = 7;}//确认修改时间
		}
		return IR_keyNumber;
}


unsigned char IR_Key()
{
	unsigned char Temp=0;
	Temp = IR_GetState();
//	KeyNumber = 0;
	return Temp;
}

void SetMODE(unsigned char i)
{
	if(i==1)
	{
		MODE=0;
	}
}



void main(){
	
	Timer1_Init();
	DS18B20_ConvertT();
	T = DS18B20_ReadT();
	LCD_Init();
	DS1302_Init();
	IR_Init();
	DS1302_SetTime();
	while (1)
	{
			SetMODE(GetMODE0());
			T_Alarm();
			KeyNum = IR_Key();
//			LCD_ShowNum(2,12,KeyNum,1);
			//闹钟响铃
			Clock_Ring(KeyNum);	
		

			//按键1进行模式选择,以及模式功能
			if(KeyNum==1)
			{
				if(MODE==0){MODE=1;LCD_Clear();}
				else if(MODE==1){MODE=2;LCD_Clear();}
				else if(MODE==2){MODE=3;}
				else if(MODE==3){MODE=4;LCD_Clear();}
				else if(MODE==4){MODE=0;LCD_Clear();}
			}
			
			LCD_ShowNum(2,16,MODE,1);//模式显示
			switch(MODE)
			{
				case 0: TimeShow();break;
				case 1: TimeSet(KeyNum);break;
				case 2: Clock_Show();break;
				case 3: Clock_Set(KeyNum);break;
				case 4: T_Show();break;
			}
	}
}




//定时器
void Timer1_Routine() interrupt 3
{		
		static unsigned int T1Count1,T1Count2;
		TL1 = 0x9C;		//设置定时初始值
		TH1 = 0xFF;		//设置定时初始值
		T1Count1++;
		if(T1Count1 >= 5000){
				T1Count1=0;
				TimeSetFlash();
			Clock_SetFlash();
		}
		T1Count2++;
		if(T1Count2 >= 200){
				T1Count2=0;
				Key_Loop();
		}
		
}








